package com.sky.directdeal.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Field;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.location.Location;
import android.net.Uri;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.gson.Gson;
import com.sky.directdeal.R;
import com.sky.directdeal.app.config.model.ConfigResponse;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.crouton.Crouton;
import com.sky.directdeal.crouton.Style;
import com.sky.directdeal.manager.SharedPreferenceManager;
import com.sky.directdeal.model.CategoryResponse;

public class Utils {
	public static CategoryResponse categoryResponse;
	
	private static ConfigResponse configResponse;
	
	public static ConfigResponse getResponse() {
		return configResponse;
	}
	
	public static void setResponse(ConfigResponse response) {
		Utils.configResponse = response;
	}
	
	public static int getPlayServiceStatus(Activity activity) {
		int status = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(activity);
		return status;
	}
	
	public static void displayToast(Activity activity,String message,Style style){
		Crouton.makeText(activity, message,
				style).show();
	}
	
	public static void globalConfigurationIntitalize(Context context){
		try {
			Utils.LoadCategory(context, Constants.CATEGORY_LIST);
			Utils.LoadApi(context,Constants.DD_SERVICE_LOCAL);

		} catch (Exception e) {
		}
	}

	
	public synchronized static void LoadCategory(Context context , String fileName) {
		InputStream inputStream=null;
		try {
			inputStream = context.getResources().getAssets().open(fileName);
			LoadCategory(inputStream);
		} catch (Exception e) {
		} finally {
			if( inputStream != null  ) {
				try {
					inputStream.close();
				} catch (IOException ignore) {}
			}
		}
	}
	
	public static void LoadApi(InputStream inputStream) {
		Gson gson = new Gson();
		Reader reader = new InputStreamReader(inputStream);
		configResponse = gson.fromJson(reader, ConfigResponse.class);
	}
	
	public synchronized static void LoadApi(Context context , String fileName) {
		InputStream inputStream=null;
		try {
			inputStream = context.getResources().getAssets().open(fileName);
			LoadApi(inputStream);
		} catch (Exception e) {
		} finally {
			if( inputStream != null  ) {
				try {
					inputStream.close();
				} catch (IOException ignore) {}
			}
		}
	}
	
	public static void LoadCategory(InputStream inputStream) {
		Gson gson = new Gson();
		Reader reader = new InputStreamReader(inputStream);
		categoryResponse = gson.fromJson(reader, CategoryResponse.class);
	}
	
	 public static int getResId(String variableName, Class<?> c) {

	        Field field = null;
	        int resId = 0;
	        try {
	            field = c.getField(variableName);
	            try {
	                resId = field.getInt(null);
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return resId;

	    }
	 
	 public static Location getLastKnownLocation(Context context) {
			Location location = null;
			try {
				location = LocationProvider.getInstance().getLocationEx(context);
			} catch (Exception e) {
			}
			return location;
		}
	 
	 public static void callNumber(String number,Activity activity) {
			TelephonyManager telMgr = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
			int simState = telMgr.getSimState();
			switch (simState) {
			case TelephonyManager.SIM_STATE_ABSENT:
				Toast.makeText(activity,activity.getString(R.string.sim_not_found), Toast.LENGTH_SHORT).show();
				break;
			case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
				Toast.makeText(activity, activity.getString(R.string.network_locked), Toast.LENGTH_SHORT).show();
				break;
			case TelephonyManager.SIM_STATE_READY:
				if(number!=null) {
					String url = Constants.ACTION_CALL_VALUE+number;
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
					activity.startActivity(intent);
				}

				break;
			case TelephonyManager.SIM_STATE_UNKNOWN:
				Toast.makeText(activity, activity.getString(R.string.unknown_simcard), Toast.LENGTH_SHORT).show();
				break;
			}
		}
	 
	 public static String getAndroidSecureId(Context context){
		 String android_id=SharedPreferenceManager.getPreferenceStringValue(context, SharedPreferenceManager.Keys.ANDROID_DEVICE_ID);
		 if(android_id!=null){
			 return android_id;
		 }
		 android_id = Secure.getString(context.getContentResolver(),
                 Secure.ANDROID_ID); 
		 SharedPreferenceManager.savePreferences(context, SharedPreferenceManager.Keys.ANDROID_DEVICE_ID, android_id);
		  return android_id;
	 }
	 
	 public static PackageInfo getPackageInfo(Context context){
		
		 try {
			PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return pInfo;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
		
	 }
	 
	 public static int getAppVersionCode(Context context){
		 if(getPackageInfo(context)!=null){
			 int versionCode=getPackageInfo(context).versionCode;
				return versionCode;
		 }
		 return 0;
	 }
	 
	 public static String getImageUrl(Context context){
		String imageUrl=Utils.getResponse().getdata().getApiservice().get(Constants.API_SERVICE_URL_KEY);
		if(imageUrl!=null){
			return imageUrl;
		}else{
			return null;
		}
	 }
	 
	 public static void ShareIntentMessage(Activity context ,String message){
		 Intent sendIntent = new Intent();
			sendIntent.setAction(android.content.Intent.ACTION_SEND);
			sendIntent.putExtra(Intent.EXTRA_TEXT,message);
			sendIntent.setType("text/plain");
			sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(Intent.createChooser(sendIntent,"Share using"));
	 }
}
